import { createRouter, createWebHistory } from 'vue-router';
import Login from '../components/Login.vue';
import Home from '../components/Home.vue';
import BasicPanel from '../components/cultivation/BasicPanel.vue';
import Welcome from '../components/welcome/Welcome';
// import MenuSetting from '../components/menu/MenuSetting';
import { getSession } from '@/util/LocalStorageUtils'
import store from '../store';
import { addMenuRouter } from '@/util/DynamicRouter'

const MenuSetting = () => import('../components/menu/MenuSetting.vue')


const routes = [
    { path: '/', redirect: '/login', name:'blank'},
    { path: '/home', name:'home', component : Home, meta: {title: "首页"}, redirect: '/welcome',
        children :[
            {path:'/welcome', name:'welcome', component : Welcome , meta : {title: "欢迎"}},
        ]
    },
    { path: '/login', name:'login', component : Login},
    { path: '/cultivation', name:'cultivation', component : BasicPanel},
    { path: '/empty', name:'empty'}
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

/**
 * 路由导航守卫 简单版
 */
let dynamicRouters = [];
router.beforeEach((to, from, next ) => {

    if (to.path === '/login'){
        return next();
    }

    const token = getSession('token');
    if (!token){
        return next('/login');
    }

    let addFlag = addMenuRouter(to.path);
    if (addFlag){
        return next({ ...to, replace: true })
    } else {
        return next();
    }

})


export default router
